package com.umakr.ax.app.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.umakr.ax.common.mapper.WechatAccountErrorLogMapper;
import com.umakr.ax.common.model.WechatAccountErrorLog;
import com.umakr.ax.common.service.WechatAccountErrorLogService;
import com.umakr.ax.core.base.BaseService;
import com.umakr.ax.wechat.util.SpeedStorage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * <p>
 * 微信公众号异常消息 服务实现类
 * </p>
 *
 * @author Wmdz
 * @since 2018-01-09
 */
@Service
public class WechatAccountErrorLogWebService extends BaseService<WechatAccountErrorLogMapper, WechatAccountErrorLog>{

    @Autowired
    WechatAccountErrorLogService wechatAccountErrorLogService;

    public Page<WechatAccountErrorLog> list(Map<String, Object> param) {
        Page<WechatAccountErrorLog> page = wechatAccountErrorLogService.query(param);
        return page;
    }

    public Map save(Map<String, Object> param) {
        Map data = new HashMap<>();
        WechatAccountErrorLog wechatAccountErrorLog = new WechatAccountErrorLog();
        wechatAccountErrorLog.setErrorInfo(param.get("errorInfo").toString());
        //wechatAccountErrorLog.setErrorInfo(new String(Base64.getEncoder().encode(param.get("errorInfo").toString().getBytes())));//base64压缩
        wechatAccountErrorLog.setErrorCode(param.get("errorCode").toString());
        wechatAccountErrorLog.setErrorDesc(param.get("errorDesc").toString());
        wechatAccountErrorLog.setRsId((param.get("rsId") != null) ? Long.parseLong(param.get("rsId").toString()) : null);
        wechatAccountErrorLog.setWaId((param.get("waId") != null) ? Long.parseLong(param.get("waId").toString()) : null);
        //默认添加的时候是未处理
        wechatAccountErrorLog.setStatus(0);
        wechatAccountErrorLog.setCreateTime(SpeedStorage.getSystemDate());
        Boolean result = wechatAccountErrorLogService.insert(wechatAccountErrorLog);
        if (result) {
            return resultMap(1, "");
        } else {
            return resultMap(0, "");
        }
    }


    public Map<String, Object> handle(Map<String, Object> param) {
        WechatAccountErrorLog wechatAccountErrorLog = wechatAccountErrorLogService.selectById(Long.parseLong(param.get("id").toString()));
        wechatAccountErrorLog.setStatus(Integer.parseInt(param.get("status").toString()));
        wechatAccountErrorLog.setUpdateTime(SpeedStorage.getSystemDate());
        wechatAccountErrorLog = wechatAccountErrorLogService.update(wechatAccountErrorLog);
        return resultMap(wechatAccountErrorLog);
    }

    public Map warning(Map<String, Object> param) {
        WechatAccountErrorLog wechatAccountErrorLog = wechatAccountErrorLogService.selectById(Long.parseLong(param.get("id").toString()));
        String mobild = param.get("mobile").toString();
        String content = param.get("content").toString();
        //TODO 调用短信发送接口
        return null;
    }
}
